home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-09-14 | 2.0 KB | 72 lines | [TEXT/MPS ] |
- /* This is test.g which tests a simple DLG-based scanner */
-
- /* ANTLR will assign token type numbers (by creating an enum which you
- * get in tokens.h)
- */
-
- <<
- /* user must define ANTLRToken here */
- class ANTLRToken : public DLGBasedToken {
- /* The base class we have chosen holds only a token type. */
-
- protected:
- /* For our simple purposes, a token and a string is enough in
- * our attribute */
- ANTLRChar _text[30];
-
- public:
- ANTLRToken(TokenType t, ANTLRChar *s) : DLGBasedToken(t) { ; }
- /* Your derived class MUST have a blank constructor. */
- ANTLRToken() {;}
-
- ANTLRChar *getText() { return _text; }
- void setText(ANTLRChar *s) { strncpy(_text, s, 30); }
-
- /* WARNING WARNING WARNING: you must return a stream of distinct tokens */
- virtual ANTLRLightweightToken *makeToken(TokenType tt, ANTLRChar *txt, int line)
- {
- ANTLRToken *t = new ANTLRToken;
- t->setType(tt); t->setText(txt); t->setLine(line);
- return t;
- }
- };
-
- /* "DLGLexer" must match what you use on DLG command line (-cl);
- * "DLGLexer" is the default.
- */
- #include "DLGLexer.h" /* include definition of DLGLexer.
- * This cannot be generated automatically because
- * ANTLR has no idea what you will call this file
- * with the DLG command-line options.
- */
-
- main()
- {
- DLGFileInput in(stdin); /* create an input stream for DLG to get chars from */
- DLGLexer scan(&in); /* create scanner reading from stdin */
- ANTLRTokenBuffer pipe(&scan);/* make a buffered pipe between lexer & parser */
- ANTLRToken aToken; /* create a token to fill in for DLG */
- scan.setToken(&aToken);
- Expr parser(&pipe); /* create a parser of type Expr hooked to the scanner */
- parser.init(); /* init the parser; prime lookahead etc... */
-
- parser.e(); /* start parsing at rule 'e' of that parser */
- }
- >>
-
- #token "[\ \t\n]+" <<skip();>>
- #token Eof "@"
-
- #tokclass My { IDENTIFIER NUMBER }
-
- class Expr { /* Define a grammar class */
-
- e : My My Eof
- <<fprintf(stderr, "text is %s,%s\n", $1->getText(), $2->getText());>>
- ;
-
- }
-
- #token IDENTIFIER "[a-z]+"
- #token NUMBER "[0-9]+"
-